<?php

namespace ticket\modules\backend\models;

use Yii;

/**
 * This is the model class for table "tk_ticket_additional".
 *
 * @property int $nid
 * @property int $tid
 * @property string $image_url
 * @property string $albums_url
 * @property string $describe
 * @property string $product_intr
 * @property string $buy_notice
 * @property string $free_peo
 * @property string $is_refund
 * @property string $belong
 * @property string $additional
 * @property string $morn_reserve_time
 * @property string $after_reserve_time
 */
class TicketAdditional extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'tk_ticket_additional';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['tid'], 'required'],
            [['tid','channel_id'], 'integer'],
            [['is_refund', 'product_intr', 'buy_notice'], 'string'],
            [['image_url', 'describe','reserve_time'], 'string', 'max' => 255],
            [['albums_url'], 'string', 'max' => 1024],
            [['free_peo'], 'string', 'max' => 512],
            [['belong'], 'string', 'max' => 64],
            [['morn_reserve_time','after_reserve_time'], 'string', 'max' => 32],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'nid' => 'Nid',
            'tid' => '门票id',
            'image_url' => '封面图url',
            'albums_url' => '轮播图 Url',
            'describe' => '描述',
            'product_intr' => '产品介绍',
            'buy_notice' => '购买须知',
            'free_peo' => '免票对象',
            'is_refund' => '是否可以退票',
            'belong' => '所属景区',
            'reserve_time' => '预约时间',
        ];
    }

    /*** 添加门票其他信息
     * @param $tid
     * @param $parmas
     * @return bool
     */
    public function additionalInfo($tid,$params)
    {
        $params['ticketInfo'] = $params;
        $data = $this->setData($params);
        $data['TicketAdditional']['tid'] = $tid;
        if  ($this->load($data) and $this->validate()){
            if ($this->save(0)){
                return true;
            }
        }

        return false;
    }

    /*** 修改门票附加数据
     * @param $tid
     * @param $params
     * @return bool
     */
    public function moditionalInfo($tid,$params)
    {
        $data = $this->setData($params);
        $data['TicketAdditional']['tid'] = $tid;
        if  ($this->load($data) and $this->validate()){
            $model = self::find()->where('tid = :id',[':id' => $tid])->one();
            if (!is_null($model)){
                $model->image_url = $data['TicketAdditional']['image_url'];
                $model->albums_url = $data['TicketAdditional']['albums_url'];
                $model->describe = $data['TicketAdditional']['describe'];
                $model->product_intr = $data['TicketAdditional']['product_intr'];
                $model->buy_notice = $data['TicketAdditional']['buy_notice'];
                $model->free_peo = $data['TicketAdditional']['free_peo'];
                $model->is_refund = $data['TicketAdditional']['is_refund'];
                $model->belong = $data['TicketAdditional']['belong'];
                $model->morn_reserve_time = $data['TicketAdditional']['morn_reserve_time'];
                $model->after_reserve_time = $data['TicketAdditional']['after_reserve_time'];
                $model->reserve_time = $data['TicketAdditional']['reserve_time'];
                $model->channel_id = $data['TicketAdditional']['channel_id'];
                if ($model->save(0)){
                    return true;
                }
            }

        }

        return false;
    }

    /*** 删除门票附加数据
     * @param $tid
     * @return bool
     * @throws \Throwable
     * @throws \yii\db\StaleObjectException
     */
    public function delTicketAdditional($tid)
    {
        $model = self::find()->where('tid = :id',[':id' => $tid])->one();
        if  (!is_null($model)){
            if ($model->delete()){
                return true;
            }
        }
        return false;
    }
    
    

    private function setData($params)
    {

        if (isset($params['ticketInfo']['image_url']) and !empty($params['ticketInfo']['image_url'])){
            $data['TicketAdditional']['image_url'] = json_encode($params['ticketInfo']['image_url']);
        }else{
            $data['TicketAdditional']['image_url'] = '';
        }
        if (isset($params['ticketInfo']['albums_url']) and !empty($params['ticketInfo']['albums_url'])){
            $data['TicketAdditional']['albums_url'] = json_encode($params['ticketInfo']['albums_url']);
        }else{
            $data['TicketAdditional']['albums_url'] = '';
        }
        if (isset($params['ticketInfo']['describe']) and !empty($params['ticketInfo']['describe'])){
            $data['TicketAdditional']['describe'] = $params['ticketInfo']['describe'];
        }else{
            $data['TicketAdditional']['describe'] = '';
        }
        if (isset($params['ticketInfo']['product_intr']) and !empty($params['ticketInfo']['product_intr'])){
            $data['TicketAdditional']['product_intr'] = $params['ticketInfo']['product_intr'];
        }else{
            $data['TicketAdditional']['product_intr'] = '';
        }
        if (isset($params['ticketInfo']['buy_notice']) and !empty($params['ticketInfo']['buy_notice'])){
            $data['TicketAdditional']['buy_notice'] = $params['ticketInfo']['buy_notice'];
        }else{
            $data['TicketAdditional']['buy_notice'] = '';
        }
        if (isset($params['ticketInfo']['free_peo']) and !empty($params['ticketInfo']['free_peo'])){
            $data['TicketAdditional']['free_peo'] = $params['ticketInfo']['free_peo'];
        }else{
            $data['TicketAdditional']['free_peo'] = '';
        }
        if (isset($params['ticketInfo']['is_refund']) and !empty($params['ticketInfo']['is_refund'])){
            $data['TicketAdditional']['is_refund'] = $params['ticketInfo']['is_refund'];
        }else{
            $data['TicketAdditional']['is_refund'] = '1';
        }
        if (isset($params['ticketInfo']['belong']) and !empty($params['ticketInfo']['belong'])){
            $data['TicketAdditional']['belong'] = $params['ticketInfo']['belong'];
        }else{
            $data['TicketAdditional']['belong'] = '';
        }
        if (isset($params['ticketInfo']['morn_reserve_time']) and !empty($params['ticketInfo']['morn_reserve_time'])){
            $data['TicketAdditional']['morn_reserve_time'] = $params['ticketInfo']['morn_reserve_time'];
        }else{
            $data['TicketAdditional']['morn_reserve_time'] = '';
        }
        if (isset($params['ticketInfo']['after_reserve_time']) and !empty($params['ticketInfo']['after_reserve_time'])){
            $data['TicketAdditional']['after_reserve_time'] = $params['ticketInfo']['after_reserve_time'];
        }else{
            $data['TicketAdditional']['after_reserve_time'] = '';
        }
        if (isset($params['ticketInfo']['reserve_time']) and !empty($params['ticketInfo']['reserve_time'])){
            $data['TicketAdditional']['reserve_time'] = $params['ticketInfo']['reserve_time'];
        }else{
            $data['TicketAdditional']['reserve_time'] = '';
        }
        if (isset($params['ticketInfo']['channel_id']) and !empty($params['ticketInfo']['channel_id'])){
            $data['TicketAdditional']['channel_id'] = $params['ticketInfo']['channel_id'];
        }else{
            $data['TicketAdditional']['channel_id'] = '';
        }

        return $data;
    }


}
